unit uConsulta;

interface

Uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,Dialogs, StdCtrls, Grids, DBGrids,
   MMSystem, Forms, Datasnap.DBClient, System.TypInfo;

   Function ConsultaCds(StrSql: string; intCds: Integer): TClientDataSet;
   Procedure GravaSql(StrTexto: String; NomeComponente: TComponentName);

implementation

uses uDmCon;

Function ConsultaCds(StrSql: string; intCds: Integer): TClientDataSet;
begin
   try
      case intCds of
         1: begin
            dmCon.cdsCon.Close;
            dmCon.cdsCon.CommandText := StrSql;
            dmCon.cdsCon.Open;
            dmCon.cdsCon.First;
         end;
         2: begin
            dmCon.cdsCon2.Close;
            dmCon.cdsCon2.CommandText := StrSql;
            dmCon.cdsCon2.Open;
            dmCon.cdsCon2.First;
         end;
         3: begin
            dmCon.cdsCon3.Close;
            dmCon.cdsCon3.CommandText := StrSql;
            dmCon.cdsCon3.Open;
            dmCon.cdsCon3.First;
         end;
      end;
     // GravaSql(StrSql, 'dmCon.cdsCon'+IntToStr(intCds));
   Except
      GravaSql(StrSql, 'dmCon.cdsCon'+IntToStr(intCds));
   end;
end;

procedure GravaSql(StrTexto: String; NomeComponente: TComponentName);
var
   Arq                     : TextFile;
   nomeArq, Linha, Caminho : String;
   ch                      : Char;
   Rect                    : TRect;
   TD                      : PTypeData;
begin
   Caminho := ExtractFilePath(Application.ExeName)+ 'qry.sql';
//   TD := GetTypeData(Screen.ActiveForm.ClassInfo); // ActiveForm.ClassInfo
   AssignFile(Arq, Caminho);

   if not FileExists(CAMINHO) then
      Rewrite(arq) else
         Append(arq);

   Writeln(arq, '');
   Writeln(arq, '/*----------------------------------------------------------------------------------------------------*/');
   Writeln(arq, '');
   Writeln(arq, StrTexto);
   Writeln(arq, '');
   Writeln(arq, uppercase('/* Data Hora: '+FormatDateTime('dd/mm/yyy hh:mm:ss', Now)+' - Componente: '+NomeComponente+ ' - Form: '+uppercase(Screen.ActiveForm.Name)+' - Unit: '+uppercase(TD^.UnitName)+' */'));
   CloseFile(Arq);
end;


end.
